﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Dynamic;
using Eleanor.BLL;
using Eleanor.Website.Utility;

namespace Eleanor.Website.Controllers
{
    public class PaintingController : EleanorControllerBase
    {
        private PaintingBLL paintingBLL = new PaintingBLL();
        private CatalogBLL catalogBLL = new CatalogBLL();
        private AuthorshipBLL authorshipBLL = new AuthorshipBLL();
        private SubjectBLL subjectBLL = new SubjectBLL();
        private TagBLL tagBLL = new TagBLL();

        public ActionResult Index(string paintingInfo)
        {
            return View();
        }

        public ActionResult List(string id)
        {
            PaintingParameters.FromPathString(id);
            int frames = 0;
            int.TryParse(PaintingParameters["frames"], out frames);
            dynamic catalog = catalogBLL.Get(PaintingParameters["catalog"]);
            int catalogId = catalog == null ? 0 : catalog.CatalogId;

            dynamic subject = subjectBLL.Get(PaintingParameters["subject"]);
            int subjectId = subject == null ? 0 : subject.SubjectId;

            dynamic authorship = authorshipBLL.Get(PaintingParameters["authorship"]);
            int authorshipId = authorship == null ? 0 : authorship.AuthorshipId;

            //构造查询条件
            dynamic condition = new ExpandoObject();
            if (catalogId == 1)
            {
                condition.PrinterEnable = true;
            }
            else
            {
                condition.HandEnable = true;
            }
            condition.SubjectId = subjectId;
            condition.AuthorshipId = authorshipId;
            //condition.Keyword = keyword;

            PaintingBLL paintingBLL = new PaintingBLL();
            int pageIndex = 1;
            if (!int.TryParse(Request.QueryString["pageindex"], out pageIndex))
            {
                pageIndex = 1;
            }
            int pageSize = SettingBLL.Instance.GetPageSizeOfPaintingList();
            dynamic paingingSearchResult = paintingBLL.Search(condition, pageIndex, pageSize);
            dynamic pageModel = new ExpandoObject();
            pageModel.PageIndex = pageIndex;
            pageModel.PageCount = (int)Math.Ceiling(paingingSearchResult.RowCount * 1.0 / pageSize);
            pageModel.PageParameter = "pageindex";
            pageModel.Path = Request.FilePath;
            pageModel.Query = Request.QueryString.ToString();
            ViewBag.Catalog = catalog;
            ViewBag.AuthorshipList = authorshipBLL.GetAll();
            ViewBag.FrameList = paintingBLL.GetAllFrames();
            ViewBag.PaintingList = paingingSearchResult.PaintingList;
            ViewBag.PageModel = pageModel;
            ViewBag.NavigationName = catalogId == 1 ? "printart" : "oilprinting";

            ViewBag.Title = catalog.Name + " | " + SettingBLL.Instance.GetWebsiteName();
            ViewBag.Keyword = SettingBLL.Instance.GetWebsiteDescription();
            ViewBag.Descripton = catalog.Description;

            return View("List");
        }

        public ActionResult Detail(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {

            }
            string catalogName = Request.QueryString["catalog"] ?? "print art";
            dynamic catalog = catalogBLL.Get(catalogName);
            int catalogId = catalog == null ? 0 : catalog.CatalogId;

            string[] parameters = id.Split("-"[0]);
            string paintingNumber = parameters[0];

            dynamic painting = paintingBLL.GetPainting(paintingNumber);
            ViewBag.AuthorshipList = authorshipBLL.GetAll();
            ViewBag.FrameList = paintingBLL.GetAllFrames();
            ViewBag.Tags = paintingBLL.GetTags(painting.PaintingId);
            ViewBag.Painting = painting;
            ViewBag.Catalog = catalog;
            ViewBag.NavigationName = catalogId == 1 ? "printart" : "oilprinting";
            ViewBag.RelatedPaintings = paintingBLL.GetRelatedPaintings(painting.PaintingId);


            ViewBag.Title = painting.Title + " | " + SettingBLL.Instance.GetWebsiteName();
            ViewBag.Description = painting.Description;
            ViewBag.Keyword = painting.Tags;
            
            return View();
        }

        public ActionResult Search(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                id = Request.QueryString["keyword"];
            }

            dynamic catalog = catalogBLL.Get(PaintingParameters["catalog"]);
            int catalogId = catalog == null ? 0 : catalog.CatalogId;

            //构造查询条件
            dynamic condition = new ExpandoObject();
            //condition.HandEnable = catalogId == 1 ? true : false;
            //condition.PrinterEnable = catalogId == 1 ? false : true;
            condition.SubjectId = 0;
            condition.AuthorshipId = 0;
            condition.Keyword = HttpUtility.UrlDecode(id);

            PaintingBLL paintingBLL = new PaintingBLL();
            int pageIndex = 1;
            if (!int.TryParse(Request.QueryString["pageindex"], out pageIndex))
            {
                pageIndex = 1;
            }
            int pageSize = SettingBLL.Instance.GetPageSizeOfPaintingList();
            dynamic paingingSearchResult = paintingBLL.Search(condition, pageIndex, pageSize);
            dynamic pageModel = new ExpandoObject();
            pageModel.PageIndex = pageIndex;
            pageModel.PageCount = (int)Math.Ceiling(paingingSearchResult.RowCount * 1.0 / pageSize);
            pageModel.PageParameter = "pageindex";
            pageModel.Path = Request.FilePath;
            pageModel.Query = Request.QueryString.ToString();
            ViewBag.AuthorshipList = authorshipBLL.GetAll();
            ViewBag.FrameList = paintingBLL.GetAllFrames();
            ViewBag.PaintingList = paingingSearchResult.PaintingList;
            ViewBag.Catalog = catalog;
            ViewBag.PageModel = pageModel;
            ViewBag.NavigationName = catalogId == 1 ? "printart" : "oilprinting";
            return View("List");
        }

    }
}
